package cn.itcast.ssh.web.action;

import cn.itcast.ssh.service.ProcessDefinitionService;
import cn.itcast.ssh.utils.ValueContext;
import org.activiti.engine.repository.ProcessDefinition;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * @author xiongshiyan at 2021/3/16 , contact me with email yanshixiong@126.com or phone 15208384257
 */
public class ProcessDefinitionAction extends BaseAction<Object>{
    private ProcessDefinitionService processDefinitionService;

    private InputStream inputStream;
    private String id;
    private String key;
    private File upload;
    private String uploadFileName;

    /**
     * 列表，显示所有流程定义的最新版本
     */
    public String list(){
        List<ProcessDefinition> processDefinitionList = processDefinitionService.findAllLatestVersions();
        ValueContext.putValueContext("processDefinitionList", processDefinitionList);
        return "list";
    }
    /**
     * 删除，指定的所有版本流程定义都会被删除
     */
    public String delete(){
        processDefinitionService.deleteByKey(key);
        return "toList";
    }
    /**
     * 添加页面
     */
    public String addUI(){
        return "addUI";
    }
    /**
     * 添加（zip格式的压缩包，包含bpmn文件和png文件）
     */
    public String add() throws IOException{
        InputStream in = new FileInputStream(upload);
        ZipInputStream zipInputStream = new ZipInputStream(in);
        processDefinitionService.deployByZip(uploadFileName, zipInputStream);
        return "toList";
    }
    /**
     * 查看流程图
     */
    public String showProcessImage(){
        inputStream = processDefinitionService.getProcessImageAsStream(id);
        return "showProcessImage";
    }


    public ProcessDefinitionService getProcessDefinitionService() {
        return processDefinitionService;
    }

    public void setProcessDefinitionService(ProcessDefinitionService processDefinitionService) {
        this.processDefinitionService = processDefinitionService;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
}
